package net.oschina.victor.liu.lemon.service;

import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import net.oschina.victor.liu.lemon.common.MessageException;
import net.oschina.victor.liu.lemon.entity.Role;
import net.oschina.victor.liu.lemon.repsotory.RoleRepsotory;

@Service
public class RoleServiceImpl implements RoleService {
	@Autowired
	private RoleRepsotory roleRepsotory;

	@Transactional
	public Boolean save(Role role) throws MessageException {
		Specification<Role> specification = new Specification<Role>() {
			@Override
			public Predicate toPredicate(Root<Role> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate p = cb.equal(root.get("role"), role.getRole());
				return p;
			}
		};
		if (roleRepsotory.findOne(specification) != null) {
			throw new MessageException("角色名称重复");
		}
		try {
			role.setCreateDate(new Date());
			roleRepsotory.save(role);
		} catch (Exception e) {
			throw new MessageException("角色信息保存失败");
		}
		return true;
	}

	@Transactional
	public Boolean update(Role role) throws MessageException {
		try {
			if (roleRepsotory.findOne(role.getId()) != null) {
				role.setUpdateDate(new Date());
				roleRepsotory.save(role);
			}
		} catch (Exception e) {
			throw new MessageException("角色信息更新保存失败");
		}
		return true;
	}

	@Transactional
	public Boolean delete(Long id) throws MessageException {
		try {
			if (roleRepsotory.findOne(id) != null) {
				roleRepsotory.delete(id);
			}
		} catch (Exception e) {
			throw new MessageException("角色信息更新保存失败");
		}
		return true;
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public Role getRole(Long id) {
		return roleRepsotory.findOne(id);
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public Page<Role> list(String keyword, int pageNo, int pageSize) {
		Specification<Role> specification = new Specification<Role>() {
			@Override
			public Predicate toPredicate(Root<Role> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate predicate = cb.or(cb.like(root.get("role"), "%" + keyword + "%"),
						cb.like(root.get("role"), "%" + keyword + "%"));
				return predicate;
			}
		};
		return roleRepsotory.findAll(specification, new PageRequest(pageNo, pageSize));
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public List<Role> listAll() {
		Specification<Role> specification = new Specification<Role>() {
			@Override
			public Predicate toPredicate(Root<Role> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate predicate = cb.isTrue(root.get("available"));
				return predicate;
			}
		};
		return roleRepsotory.findAll(specification);
	}

}
